package com.netease.nie.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.netease.nie.base.controllers.BaseController;
import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.dto.GoodsExchangeRequestDto;
import com.netease.nie.dto.GoodsExchangeResponseDto;
import com.netease.nie.facade.GoodsExchangeFacade;

/**
 * 商品兑换接口
 * @author wwbn3695
 * @createDate July 14th,2016
 *
 */
@RestController
@RequestMapping("/api")
public class GoodsExchangeController extends BaseController {

	@Autowired
	private GoodsExchangeFacade goodsExchangeFacade;
	
	@RequestMapping(value = "/goods_exchange", method = RequestMethod.POST, produces = "application/json")
	public String goodsExchange(HttpServletRequest request, @RequestBody GoodsExchangeRequestDto goodsExchangeRequestDto) throws Exception {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			throw new BusinessException(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		if (userId <= 0)
			throw new BusinessException(BusinessConstants.INVALID_USER_ID.getCode(), "用户id不合法");
		Integer exchangeGoodsId = goodsExchangeRequestDto.getGoodsId();		
		if (exchangeGoodsId <= 0 || exchangeGoodsId == null)
			throw new BusinessException(BusinessConstants.GOODS_NOT_EXISTS.getCode(), "商品id不合法");		
		
		GoodsExchangeResponseDto goodsExchangeResponseDto = goodsExchangeFacade.goodsExchange(userId, goodsExchangeRequestDto);
		return jsonSuccess(goodsExchangeResponseDto);
	}
}
